package com.zz.biz.inventory.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.R;
import com.zz.biz.inventory.domain.SmdInventory;
import com.zz.dto.InventoryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.zz.biz.inventory.service.ISmdInventoryService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 库存管理Controller
 * 
 * @author zz
 * @date 2025-06-25
 */
@RestController
@RequestMapping("/inventory")
@Slf4j
public class SmdInventoryController extends BaseController
{
    @Autowired
    private ISmdInventoryService smdInventoryService;

    @RequiresPermissions("biz:inventory:isinstock")
    @PostMapping("/isinstock")
    public R<List<InventoryResponse>> isInStock(@RequestBody List<Map<String,Object>> skuCode) {
        log.info("查询库存：{}", JSON.toJSONString(skuCode));
        return smdInventoryService.isInStock(skuCode);
    }

    /**
     * 查询库存管理列表
     */
    @RequiresPermissions("biz:inventory:list")
    @GetMapping("/list")
    public TableDataInfo list(SmdInventory smdInventory)
    {
        startPage();
        List<SmdInventory> list = smdInventoryService.selectSmdInventoryList(smdInventory);
        return getDataTable(list);
    }

    /**
     * 导出库存管理列表
     */
    @RequiresPermissions("biz:inventory:export")
    @Log(title = "库存管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmdInventory smdInventory)
    {
        List<SmdInventory> list = smdInventoryService.selectSmdInventoryList(smdInventory);
        ExcelUtil<SmdInventory> util = new ExcelUtil<SmdInventory>(SmdInventory.class);
        util.exportExcel(response, list, "库存管理数据");
    }

    /**
     * 获取库存管理详细信息
     */
    @RequiresPermissions("biz:inventory:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(smdInventoryService.selectSmdInventoryById(id));
    }

    /**
     * 新增库存管理
     */
    @RequiresPermissions("biz:inventory:add")
    @Log(title = "库存管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmdInventory smdInventory)
    {
        return toAjax(smdInventoryService.insertSmdInventory(smdInventory));
    }

    /**
     * 修改库存管理
     */
    @RequiresPermissions("biz:inventory:edit")
    @Log(title = "库存管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmdInventory smdInventory)
    {
        return toAjax(smdInventoryService.updateSmdInventory(smdInventory));
    }

    /**
     * 删除库存管理
     */
    @RequiresPermissions("biz:inventory:remove")
    @Log(title = "库存管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(smdInventoryService.deleteSmdInventoryByIds(ids));
    }
}
